package com.gec.zhs.controller;

import com.gec.zhs.component.EsSearchComponent;
import com.gec.zhs.component.RedisComponent;
import com.gec.zhs.entity.constants.Constants;
import com.gec.zhs.entity.dto.TokenUserInfoDto;
import com.gec.zhs.entity.enums.*;
import com.gec.zhs.entity.pojo.UserAction;
import com.gec.zhs.entity.pojo.VideoInfo;
import com.gec.zhs.entity.pojo.VideoInfoFile;
import com.gec.zhs.entity.query.UserActionQuery;
import com.gec.zhs.entity.query.VideoInfoFileQuery;
import com.gec.zhs.entity.query.VideoInfoQuery;
import com.gec.zhs.entity.vo.PaginationResultVO;
import com.gec.zhs.entity.vo.ResponseVO;
import com.gec.zhs.entity.vo.VideoInfoResultVo;
import com.gec.zhs.entity.vo.VideoInfoVo;
import com.gec.zhs.exception.BusinessException;
import com.gec.zhs.service.UserActionService;
import com.gec.zhs.service.VideoInfoFileService;
import com.gec.zhs.service.VideoInfoService;
import com.gec.zhs.utils.CopyTools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhs
 * @version 1.0
 * @date 2025-02-13
 * @description
 */
@RestController
@RequestMapping("/video")
public class VideoController extends ABaseController{

    @Autowired
    private VideoInfoService videoInfoService;

    @Autowired
    private VideoInfoFileService videoInfoFileService;

    @Autowired
    private UserActionService userActionService;

    @Autowired
    private RedisComponent redisComponent;

    @Autowired
    private EsSearchComponent  esSearchComponent;

    @ApiOperation(value = "获取推荐视频")
    @RequestMapping("/loadRecommendVideo")
    public ResponseVO loadRecommendVideo() {
        VideoInfoQuery videoInfoQuery = new VideoInfoQuery();
        videoInfoQuery.setOrderBy("create_time desc");
        videoInfoQuery.setQueryUserInfo(true);
        videoInfoQuery.setRecommendType(VideoRecommendTypeEnum.RECOMMEND.getType());
        List<VideoInfo> videoInfoList = videoInfoService.findListByParam(videoInfoQuery);
        return getSuccessResponseVO(videoInfoList);
    }

    @ApiOperation(value = "获取未推荐的视频")
    @RequestMapping("/loadVideo")
    public ResponseVO loadVideo(Integer pCategoryId,Integer categoryId,Integer pageNo) {
        VideoInfoQuery videoInfoQuery = new VideoInfoQuery();
        videoInfoQuery.setPCategoryId(pCategoryId);
        videoInfoQuery.setCategoryId(categoryId);
        videoInfoQuery.setPageNo(pageNo);
        videoInfoQuery.setOrderBy("create_time desc");
        videoInfoQuery.setQueryUserInfo(true);
        if (categoryId == null && pCategoryId == null) {
            videoInfoQuery.setRecommendType(VideoRecommendTypeEnum.NO_RECOMMEND.getType());
        }
        PaginationResultVO<VideoInfo> videoInfoServiceListByPage = videoInfoService.findListByPage(videoInfoQuery);
        return getSuccessResponseVO(videoInfoServiceListByPage);
    }

    @ApiOperation(value = "获取视频详情数据")
    @RequestMapping("/getVideoInfo")
    public ResponseVO getVideoInfo(String videoId) {
        VideoInfo videoInfo = videoInfoService.getVideoInfoByVideoId(videoId);
        if (null == videoInfo) {
            throw new BusinessException(ResponseCodeEnum.CODE_404);
        }
        TokenUserInfoDto userInfoDto = getTokenUserInfoDto();
        //用户的行为列表,一键三连
        List<UserAction> userActionList = new ArrayList<>();
        if (userInfoDto != null) {
            UserActionQuery actionQuery = new UserActionQuery();
            actionQuery.setVideoId(videoId);
            actionQuery.setUserId(userInfoDto.getUserId());
            actionQuery.setActionTypeArray(new Integer[]{UserActionTypeEnum.VIDEO_LIKE.getType(), UserActionTypeEnum.VIDEO_COLLECT.getType(),
                    UserActionTypeEnum.VIDEO_COIN.getType(),});
            userActionList = userActionService.findListByParam(actionQuery);
        }
        VideoInfoResultVo resultVo = new VideoInfoResultVo();
        resultVo.setVideoInfo(CopyTools.copy(videoInfo, VideoInfoVo.class));
        resultVo.setUserActionList(userActionList);
        return getSuccessResponseVO(resultVo);
    }

    //视频分p数据，p1,p2,p3...
    @ApiOperation(value = "获取视频分p数据")
    @RequestMapping("/loadVideoPList")
    public ResponseVO loadVideoPList(String videoId) {
        VideoInfoFileQuery videoInfoFileQuery = new VideoInfoFileQuery();
        videoInfoFileQuery.setVideoId(videoId);
        videoInfoFileQuery.setOrderBy("file_index asc");
        List<VideoInfoFile> videoInfoFileList = videoInfoFileService.findListByParam(videoInfoFileQuery);
        return getSuccessResponseVO(videoInfoFileList);
    }

    /**
     * 利用redis的setex来模拟心跳检测机制
     * 心跳机制 检测用户是否在线观看视频 ， 统计用户总数
     * @param fileId 视频文件id
     * @param deviceId 浏览器id
     * @return
     */
    @ApiOperation(value = "在线播放视频")
    @RequestMapping("/reportVideoPlayOnline")
    public ResponseVO reportVideoPlayOnline(String fileId,String deviceId) {
        return getSuccessResponseVO(redisComponent.reportVideoPlayOnline(fileId, deviceId));
    }

    @ApiOperation("首页es搜索视频的功能")
    @RequestMapping("/search")
    public ResponseVO esSearchVideo(String keyword,Integer orderType,Integer pageNo) {
        PaginationResultVO<VideoInfo> videoInfoPage = esSearchComponent.search(true, keyword, orderType, pageNo, PageSize.SIZE30.getSize());
        //记录搜索热词
        redisComponent.addKeywordCount(keyword);
        return getSuccessResponseVO(videoInfoPage);
    }

    //es会根据分词，算分，查数据库，展示
    @ApiOperation("视频页面es搜索侧边推荐视频")
    @RequestMapping("/getVideoRecommend")
    public ResponseVO getVideoRecommend(String keyword,String videoId) {
        PaginationResultVO<VideoInfo> videoInfoPage = esSearchComponent.search(false, keyword, SearchOrderTypeEnum.VIDEO_PLAY.getType(), 1, PageSize.SIZE15.getSize());
        List<VideoInfo> videoInfoList = videoInfoPage.getList();
        videoInfoList = videoInfoList.stream().filter(t->!t.getVideoId().equals(videoId)).collect(Collectors.toList());
        return getSuccessResponseVO(videoInfoList);
    }

    @ApiOperation("搜索栏展示搜索热词")
    @RequestMapping("/getSearchKeywordTop")
    public ResponseVO getSearchKeyWord() {
        List<String> keywordTopList = redisComponent.getKeywordTop(Constants.LENGTH_10);
        return getSuccessResponseVO(keywordTopList);
    }

    @ApiOperation("首页24小时播放数量热门")
    @RequestMapping("/loadHotVideoList")
    public ResponseVO loadHotVideoList(Integer pageNo) {
        VideoInfoQuery videoInfoQuery = new VideoInfoQuery();
        videoInfoQuery.setPageNo(pageNo);
        videoInfoQuery.setQueryUserInfo(Boolean.TRUE);
        videoInfoQuery.setOrderBy("play_count desc");
        videoInfoQuery.setLastPlayHour(Constants.HOUR_24);
        PaginationResultVO<VideoInfo> videoInfoPage = videoInfoService.findListByPage(videoInfoQuery);
        return getSuccessResponseVO(videoInfoPage);
    }
}
